﻿package mortal.game.view.mount.panel
{
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import fl.data.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.palyer.data.*;
    import mortal.game.view.wizard.*;
    import mortal.game.view.wizard.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class EquipBaseWin extends SmallWindow
    {
        protected var _packItemPanel:GTileList;
        protected var _tabar:GTabarNew;
        protected var _group:int;
        protected var _category:int;
        protected var _effect:int;
        protected var _effectEx:int;
        protected var _packPosType:int;
        protected var _targetLevel:int;
        protected var _titleDic:Dictionary;
        protected var _showType:int;
        private static var _instance:EquipBaseWin;

        public function EquipBaseWin(param1:ILayer = null)
        {
            super(param1);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            this.layer = LayerManager.topLayer;
            super.show(param1, param2);
            this.updateLevel();
            return;
        }// end function

        override protected function addWindowLine() : void
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.setSize(270, 180);
            this.titleHeight = 55;
            this.paddingBottom = 22;
            this._packItemPanel = UIFactory.tileList(20, 64, 250, 92, this);
            this._packItemPanel.rowHeight = 46;
            this._packItemPanel.columnWidth = 46;
            this._packItemPanel.horizontalGap = -1;
            this._packItemPanel.verticalGap = 0;
            this._packItemPanel.setStyle("cellRenderer", EquipWinCellRenderer);
            this._packItemPanel.selectable = false;
            this._tabar = UIFactory.gTabBarNew(2, 35, null, 270, 160, 65, 26, this, null, "TabButtonNew");
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(35438), 80, 165, 140, 20, this, GlobalStyle.textFormatHuang2));
            this.addListener();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._packItemPanel.dispose(param1);
            this._packItemPanel = null;
            this._tabar.dispose(param1);
            this._tabar = null;
            this.removeListener();
            this._titleDic = new Dictionary();
            super.disposeImpl(param1);
            return;
        }// end function

        protected function getTitleNameByType() : Array
        {
            return this._titleDic[this._showType];
        }// end function

        protected function getDataProvider() : DataProvider
        {
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_5:ItemData = null;
            var _loc_6:Object = null;
            var _loc_7:Object = null;
            var _loc_9:PackPosTypeCache = null;
            var _loc_10:ItemData = null;
            var _loc_1:int = 10;
            var _loc_2:* = new DataProvider();
            if (this._showType == 99)
            {
                _loc_3 = Cache.instance.pack.backPackCache.getItemsByGroupAndCategory(this._group, this._category, this._effect, -1, -1, this._effectEx);
            }
            else
            {
                _loc_3 = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(this._group, this._category, this._showType, this._effect, -1, -1, this._effectEx);
            }
            _loc_4 = Math.ceil(_loc_3.length / 5) * 5 >= _loc_1 ? (Math.ceil(_loc_3.length / 5) * 5) : (_loc_1);
            var _loc_8:int = 0;
            while (_loc_8 < _loc_4)
            {
                
                _loc_7 = new Object();
                if (_loc_3[_loc_8])
                {
                    _loc_7.itemData = _loc_3[_loc_8];
                    if (this._targetLevel == 0 || this._targetLevel >= (_loc_3[_loc_8] as ItemData).itemInfo.level)
                    {
                        if (ItemsUtil.isPrescriOutTime(_loc_3[_loc_8] as ItemData))
                        {
                            _loc_7.isOpened = true;
                            _loc_2.addItem(_loc_7);
                        }
                        if (_loc_5 == null || EquipmentUtil.getEquipBaseCombat(_loc_5) < EquipmentUtil.getEquipBaseCombat(_loc_3[_loc_8]))
                        {
                            _loc_5 = _loc_3[_loc_8];
                            _loc_6 = _loc_7;
                        }
                    }
                }
                else
                {
                    _loc_7.isOpened = true;
                }
                _loc_2.addItem(_loc_7);
                _loc_8++;
            }
            if (_loc_5)
            {
                _loc_9 = Cache.instance.pack.getPackChacheByPosType(this._packPosType);
                if (_loc_9)
                {
                    _loc_10 = _loc_9.getItemDataByType(this._showType);
                    if (_loc_10 == null || EquipmentUtil.getEquipBaseCombat(_loc_10) < EquipmentUtil.getEquipBaseCombat(_loc_5))
                    {
                        _loc_6.isMaxCombat = true;
                    }
                }
                else
                {
                    _loc_6.isMaxCombat = true;
                }
            }
            return _loc_2;
        }// end function

        protected function updateAllItems(param1:Object = null) : void
        {
            var _loc_2:Array = null;
            if (this._packItemPanel)
            {
                this._packItemPanel.dataProvider = this.getDataProvider();
            }
            if (this._tabar)
            {
                _loc_2 = this.getTitleNameByType();
                this._tabar.dataProvider = _loc_2;
            }
            return;
        }// end function

        protected function updateLevel(param1:Object = null) : void
        {
            var _loc_2:WizardData = null;
            var _loc_3:FashionAdvanceData = null;
            if (this._packPosType == EPlayerItemPosType._EPlayerItemPosTypeRole)
            {
                this._targetLevel = Cache.instance.role.entityInfo.level;
            }
            else if (this._packPosType == EPlayerItemPosType._EPlayerItemPosTypeMount)
            {
                this._targetLevel = Cache.instance.mount.mountData.mountLevel;
            }
            else if (this._packPosType == EPlayerItemPosType._EPlayerItemPosTypePet)
            {
                this._targetLevel = Cache.instance.newPet.pet.code;
            }
            else if (this._packPosType > EPlayerItemPosType._EPlayerItemPosTypeSprite && this._packPosType < EPlayerItemPosType._EPlayerItemPosTypeSpriteEnd)
            {
                _loc_2 = Cache.instance.wizard.getWizardDataByCode(this._effectEx);
                if (_loc_2.isHasWizard)
                {
                    this._targetLevel = _loc_2.spriteInfo.SpriteLevel;
                }
                else
                {
                    this._targetLevel = -1;
                }
            }
            else if (this._packPosType > EPlayerItemPosType._EPlayerItemPosTypeFashion && this._packPosType < EPlayerItemPosType._EPlayerItemPosTypeFashionEnd)
            {
                _loc_3 = Cache.instance.fashion.getFashionInfo(GameController.playerSystem.fashion.getCurrFashionType());
                if (_loc_3.advanceInfo && _loc_3.advanceInfo.code > 0)
                {
                    this._targetLevel = _loc_3.advanceInfo.code;
                }
                else
                {
                    this._targetLevel = -1;
                }
            }
            this.updateAllItems();
            return;
        }// end function

        protected function addListener() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updateAllItems);
            if (this._packPosType == EPlayerItemPosType._EPlayerItemPosTypeRole)
            {
                Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.updateLevel);
            }
            else if (this._packPosType == EPlayerItemPosType._EPlayerItemPosTypeMount)
            {
                NetDispatcher.addCmdListener(ServerCommand.MountLevelUp, this.updateLevel);
            }
            else if (this._packPosType == EPlayerItemPosType._EPlayerItemPosTypePet)
            {
                NetDispatcher.addCmdListener(ServerCommand.PetAdvanceBack, this.updateLevel);
            }
            else if (this._packPosType > EPlayerItemPosType._EPlayerItemPosTypeSprite && this._packPosType < EPlayerItemPosType._EPlayerItemPosTypeSpriteEnd)
            {
                NetDispatcher.addCmdListener(ServerCommand.WizardLevelUp, this.updateLevel);
                NetDispatcher.addCmdListener(ServerCommand.WizardActive, this.updateLevel);
            }
            else if (this._packPosType > EPlayerItemPosType._EPlayerItemPosTypeFashion && this._packPosType < EPlayerItemPosType._EPlayerItemPosTypeFashionEnd)
            {
                NetDispatcher.addCmdListener(ServerCommand.FashionLevelUp, this.updateLevel);
            }
            return;
        }// end function

        protected function removeListener() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updateAllItems);
            if (this._packPosType == EPlayerItemPosType._EPlayerItemPosTypeRole)
            {
                Cache.instance.role.roleEntityInfo.removeEventListener(EntityInfoEventName.UpdateLevel, this.updateLevel);
            }
            else if (this._packPosType == EPlayerItemPosType._EPlayerItemPosTypeMount)
            {
                NetDispatcher.removeCmdListener(ServerCommand.MountLevelUp, this.updateLevel);
            }
            else if (this._packPosType == EPlayerItemPosType._EPlayerItemPosTypePet)
            {
                NetDispatcher.removeCmdListener(ServerCommand.PetAdvanceBack, this.updateLevel);
            }
            else if (this._packPosType > EPlayerItemPosType._EPlayerItemPosTypeSprite && this._packPosType < EPlayerItemPosType._EPlayerItemPosTypeSpriteEnd)
            {
                NetDispatcher.removeCmdListener(ServerCommand.WizardLevelUp, this.updateLevel);
                NetDispatcher.removeCmdListener(ServerCommand.WizardActive, this.updateLevel);
            }
            else if (this._packPosType > EPlayerItemPosType._EPlayerItemPosTypeFashion && this._packPosType < EPlayerItemPosType._EPlayerItemPosTypeFashionEnd)
            {
                NetDispatcher.removeCmdListener(ServerCommand.FashionLevelUp, this.updateLevel);
            }
            return;
        }// end function

        public function setWinData(param1:Dictionary, param2:int, param3:int, param4:int, param5:int = 99, param6:int = -1, param7:int = -1) : void
        {
            this._titleDic = param1;
            this._packPosType = param2;
            this._group = param3;
            this._category = param4;
            this._showType = param5;
            this._effect = param6;
            this._effectEx = param7;
            return;
        }// end function

        public static function get instance() : EquipBaseWin
        {
            if (_instance == null)
            {
                _instance = new EquipBaseWin;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
